﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using CentralAdmin = WatiN.Sharepoint.Pages.CentralAdmin;
using SPPages = WatiN.Sharepoint.Pages;
using WebControls = WatiN.Sharepoint.WebControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MSTest.WatiN
{

    [TestClass]
    public class SPCentralAdminTests
    {
        [ClassInitialize]
        public static void WatiNSetUp(TestContext context)
        {
            SPPages.Settings.CentralAdminRootUrl = @"http://spdev01:3333/";
        }

        [TestMethod]
        public void HomePage_OpenDefaultPageAndCheckMenuItems_MonitoringItemExists()
        {
            using (Browser browser = new IE())
            {
                CentralAdmin.CentralAdminPage homePage = CentralAdmin.Navigator.Go<CentralAdmin.CentralAdminPage>(browser);

                Assert.IsTrue(homePage.QuickLaunch.MenuItems.Any(item => string.Compare("Monitoring", item.Title, StringComparison.InvariantCultureIgnoreCase) == 0));                
            }
        }

        [TestMethod]
        public void CreateSite_CreateATeamsite_SiteCreated()
        {
            using (Browser browser = new IE())
            {
                CentralAdmin.CreateSitePage createSitePage = CentralAdmin.Navigator.Go<CentralAdmin.CreateSitePage>(browser);

                string siteName = string.Format("UI.Test.Teamsite-{0}", DateTime.Now.ToString("yyyy-MM-ddtHH-mm-ss"));

                createSitePage.SiteTitle.TypeText(siteName);
                createSitePage.SiteDescription.TypeText("This is a teamsite created with a UI test");
                if (createSitePage.URL.InnerHtml.Equals("/") == false)
                {
                    createSitePage.SiteName.TypeText(siteName);
                }
                createSitePage.Language.Select("english");
                createSitePage.SelectWebTemplateGroup("Collaboration");
                createSitePage.WebTemplate.Select("Team Site");
                createSitePage.SetPrimaryAdministrator("Administrator");

                createSitePage.Submit(true);

                CentralAdmin.SiteCreatedPage siteCreatedPage = browser.Page<CentralAdmin.SiteCreatedPage>();

                Assert.IsTrue(siteCreatedPage.CreatedSiteLink.Exists, "The link to the new site should exists on the current page");
                Assert.IsTrue(siteCreatedPage.CreatedSiteLink.Url.Contains(siteName), "The link to the new site should contain the siteName specified earlier");
            }
        }

        [TestMethod]
        public void ListContentSources_blablabla_SiteCreated()
        {
            using (Browser browser = new IE())
            {
                CentralAdmin.ListContentSourcesPage contentSourcesPage = CentralAdmin.Navigator.Go<CentralAdmin.ListContentSourcesPage>(browser, new Dictionary<string, string>() { { "appid", "8b9854bf%2D79aa%2D4f88%2Db39c%2Da9a8fc1b948b" } });

                contentSourcesPage.StartIncrementalCrawl("Local Sharepoint Sites");
            }
        }
    }
}

